# Create your views here.
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseRedirect
from homebase.projects.models import Project, ToDo, Setting, GenericField

def index(request):
	priority_list = Project.objects.all().order_by('priority')
	#error = True
	#error_message = "There was an error!"
	return render_to_response('projects/viewall.html', {'project_list': priority_list.filter(completed=False)})

def view_all(request):
	priority_list = Project.objects.all().order_by('priority')
	return render_to_response('projects/viewall.html', {'project_list': priority_list})

def detail(request, project_id):
	p = get_object_or_404(Project, pk=project_id)
	
	low=0
	range = 10
	high=low+range
	#sort the todos by priority, and only get the uncompleted ones
	t = p.todo_set.all().order_by('priority')
	t = t.filter(completed=False)
	
	# by default, grab items low=0 to high=10
	#if a request was found for different ones, let us know
	if (request.GET and request.GET['low']):
		low = int(request.GET['low'])
	if (request.GET and request.GET['high']):
		high = int(request.GET['high'])
	#just make sure that high is more the low
	if (high <= low):
		high = low + range
		
	#do we display the "back to start" link for pagination?
	start = False
	if (low > 0):
		start = True
	
	#work out high and low values for the "end"
	end_low = len(t) - range
	end_high = len(t)
	# is a end button even needed?
	end = False
	if (end_high > high ):
		end = True
	
	
	#work out the values for the next highest
	next_low=high+1
	next_high=next_low + range
	#but do we even need a "next"
	next = False
	if (next_high < end_high):
		next = True
	
	#work out the values for the next lowest
	previous_high = low - 1
	previous_low = previous_high - range
	
	previous = False
	if (previous_low > 0):
		previous = True
		
	return render_to_response('projects/detail.html', {'project': p, 'todo_set':t[low:high], 'low':low, 'high':high, 'start':start, 'end_low':end_low, 'end_high':end_high, 'end':end, 'next_low':next_low, 'next_high':next_high, 'next':next, 'previous_high':previous_high, 'previous_low':previous_low, 'previous':previous, 'range':range})
	
def todo(request, project_id):
	p = get_object_or_404(Project, pk=project_id)
	todo = p.todo_set.all().order_by('priority')
	return render_to_response('projects/todo.html', {'project': p, 'todo': todo})
	
def todo_all(request, project_id):
	p = get_object_or_404(Project, pk=project_id)
	todo = p.todo_set.all().order_by('priority')
	return render_to_response('projects/todo_all.html', {'project': p, 'todo': todo})
	
	
def add(request):
	#Create a dictionary of the default values for the form
	p = {'title':"Enter title here", 'description':"Enter a short description"}
	p['url'] = "http://sample.url"
	p['completiondate'] = "2009-01-01 17:00:01"
	p['priority'] = 4
	p['version'] = "0.01"
	return render_to_response('projects/add.html', {'project':p})

def commit_new(request):
	p = Project()
	p.title=request.POST['title']
	p.description=request.POST['description']
	p.url=request.POST['url']
	p.completiondate=request.POST['completiondate']
	p.priority=request.POST['priority']
	p.version=request.POST['version']
	p.completed=0
	p.save()
	return HttpResponseRedirect('/projects/%s/' % p.id)


def project_complete(request, project_id):
	p = get_object_or_404(Project, pk=project_id)
	p.completed=1
	p.save()
	return HttpResponseRedirect('/projects/')

def settings(request):
	s=Setting.objects.all()
	return render_to_response('projects/settings.html', {'settings':s})

def commit_edit(request, project_id):
	p = get_object_or_404(Project, pk=project_id)
	p.title=request.POST['title']
	p.description=request.POST['description']
	p.url=request.POST['url']
	p.completiondate=request.POST['completiondate']
	p.priority=request.POST['priority']
	p.version=request.POST['version']
	p.save()
	return HttpResponseRedirect('/projects/%s/' % p.id)

def todo_complete(request, project_id, todo_id):
	t = get_object_or_404(ToDo, pk=todo_id)
	t.completed = True
	t.save()
	return HttpResponseRedirect('/projects/%s/' % project_id)

def todo_uncomplete(request, project_id, todo_id):
	t = get_object_or_404(ToDo, pk=todo_id)
	t.completed = False
	t.save()
	return HttpResponseRedirect('/projects/%s/' % project_id)

def todo_commit(request, project_id):
	if (len(request.POST['item1']) > 0):
		t1 = ToDo()
		t1.project_id = project_id
		t1.completed=False
		t1.item = request.POST['item1']
		if (request.POST['priority1']):
			t1.priority = int(request.POST['priority1'])
		else:
			t1.priority = 3
		t1.save()
	if (len(request.POST['item2']) > 0):
		t2 = ToDo()
		t2.project_id = project_id
		t2.completed = False
		t2.item = request.POST['item2']
		if (request.POST['priority2']):
			t2.priority = int(request.POST['priority2'])
		else:
			t2.priority = 3
		t2.save()
	if (len(request.POST['item3']) > 0):
		t3 = ToDo()
		t3.project_id = project_id
		t3.completed = False
		t3.item = request.POST['item3']
		if (request.POST['priority3']):
			t3.priority = int(request.POST['priority3'])
		else:
			t3.priority = 3
		t3.save()
	return HttpResponseRedirect('/projects/%s/' % project_id)
		
		
def generic_commit(request, project_id, generic_id):
	g = get_object_or_404(GenericField, pk=generic_id)
	g.fieldname = request.POST['fieldname']
	g.value = request.POST['value']
	g.save()
	return HttpResponseRedirect('/projects/%s/' % project_id)

def generic_add(request, project_id):
	
	if (len(request.POST['fieldname']) > 0):
		g = GenericField()
		g.project_id = project_id
		g.fieldname = request.POST['fieldname']
		g.value = request.POST['value']
		g.save()
	
	return HttpResponseRedirect('/projects/%s/' % project_id)

